package com.tech.koufu.ui.adapter;

import java.text.DecimalFormat;
import java.util.ArrayList; 
import com.tech.koufu.R; 
import com.tech.koufu.model.UserStocks;
import com.tech.koufu.tools.KouFuTools;
import com.tech.koufu.ui.activity.BuyStocksActivity;
import com.tech.koufu.ui.activity.DetailTransStocksActivity;
import com.tech.koufu.ui.activity.MarketDeatilActivity; 

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter; 
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 首页列表
 * @author 123
 *
 */
public class MainStocksAdapter extends BaseAdapter {
	public ArrayList<UserStocks> stockinfo;
	private Context context;  
	private LayoutInflater mInflater = null; 
	public MainStocksAdapter(Context context){
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	public MainStocksAdapter(Context context , ArrayList<UserStocks> stockinfo) {
		super();
		this.stockinfo = stockinfo;
		this.context = context;
		this.mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() { 
		return stockinfo.size();
	}

	@Override
	public Object getItem(int position) { 
		return stockinfo.get(position);
	}

	@Override
	public long getItemId(int position) { 
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		 ViewHolder holder = null;  
         //如果缓存convertView为空，则需要创建View  
         if(convertView == null)  
         {  
             holder = new ViewHolder();  
             //根据自定义的Item布局加载布局  
             convertView = mInflater.inflate(R.layout.lv_item_stock, parent,false); 
             holder.txt_stock_name = (TextView)convertView.findViewById(R.id.txt_stock_name);  
             holder.txt_stock_code = (TextView)convertView.findViewById(R.id.txt_stock_code);
             holder.btn_charge = (TextView)convertView.findViewById(R.id.btn_charge);  
             holder.lv_item_curprice_gains = (TextView)convertView.findViewById(R.id.lv_item_curprice_gains);
             holder.lv_item_cost_value = (TextView)convertView.findViewById(R.id.lv_item_cost_value);
             holder.lv_item_stock_num_value = (TextView)convertView.findViewById(R.id.lv_item_stock_num_value);
             holder.lv_item_breakevenamount_price = (TextView)convertView.findViewById(R.id.lv_item_breakevenamount_price);
             holder.lv_item_stock_market_price = (TextView)convertView.findViewById(R.id.lv_item_stock_market_price);
             
             holder.btn_lv_tog = (ImageView)convertView.findViewById(R.id.btn_lv_tog);
             holder.ly_lv_tog = (LinearLayout)convertView.findViewById(R.id.ly_lv_tog);
             holder.ly_item_stock_num = (RelativeLayout)convertView.findViewById(R.id.ly_item_stock_num);
             holder.ly_item_stock_market = (RelativeLayout)convertView.findViewById(R.id.ly_item_stock_market);
             
             holder.ll_item_btns = (LinearLayout)convertView.findViewById(R.id.ll_item_btns);
             holder.btnBuy = (Button)convertView.findViewById(R.id.btn_buy);
             holder.btnSell = (Button)convertView.findViewById(R.id.btn_sell);
             holder.btn_trans_details = (Button)convertView.findViewById(R.id.btn_trans_details);
             holder.btn_market = (Button)convertView.findViewById(R.id.btn_market);
             //将设置好的布局保存到缓存中，并将其设置在Tag里，以便后面方便取出Tag  
             convertView.setTag(holder);  
         }else  
         {  
             holder = (ViewHolder)convertView.getTag();  
         }  
         final View ly_item_stock_num = holder.ly_item_stock_num;
         final View ly_item_stock_market = holder.ly_item_stock_market;
         final ImageView btn_lv_tog = holder.btn_lv_tog;
         final LinearLayout llItemBtns = holder.ll_item_btns;
         holder.ly_lv_tog.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(ly_item_stock_num.getVisibility() == View.VISIBLE){
					ly_item_stock_num.setVisibility(View.GONE);
					ly_item_stock_market.setVisibility(View.GONE);
					llItemBtns.setVisibility(View.GONE);
					btn_lv_tog.setImageResource(R.drawable.btn_lv_open);
				}else{
					ly_item_stock_num.setVisibility(View.VISIBLE);
					ly_item_stock_market.setVisibility(View.VISIBLE); 
					llItemBtns.setVisibility(View.VISIBLE);
					btn_lv_tog.setImageResource(R.drawable.btn_lv_close);
				}
			}
		});
         convertView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(ly_item_stock_num.getVisibility() == View.VISIBLE){
					ly_item_stock_num.setVisibility(View.GONE);
					ly_item_stock_market.setVisibility(View.GONE);
					llItemBtns.setVisibility(View.GONE);
					btn_lv_tog.setImageResource(R.drawable.btn_lv_open);
				}else{
					ly_item_stock_num.setVisibility(View.VISIBLE);
					ly_item_stock_market.setVisibility(View.VISIBLE); 
					llItemBtns.setVisibility(View.VISIBLE);
					btn_lv_tog.setImageResource(R.drawable.btn_lv_close);
				} 
			}
		});
         ItemClick onclick =  new ItemClick(position);
         holder.btnBuy.setOnClickListener(onclick);
         holder.btnSell.setOnClickListener(onclick);
         holder.btn_trans_details.setOnClickListener(onclick);
         holder.btn_market.setOnClickListener(onclick);
         
         try {
			UserStocks info = (UserStocks)getItem(position);
			 DecimalFormat decimalFormat=new DecimalFormat("##0.00");
//         Log.w("Tt", info.getStockdqj()+">>"+info.getStockHold()+">"+info.getStockbj()+">");
			 String dqj = info.getStockdqj()!=null?info.getStockdqj():"0";        
			 //昨收盘
			 String zsp = info.getStockZsp()!=null ? info.getStockZsp():"0"; 
			 Log.e("Tt", "dqj="+dqj+",zsp="+zsp);
			 if(dqj.equals("0")|| (dqj!=null && Float.parseFloat(dqj)==0)){
				 dqj = zsp;
			 }
			 Float charge = Float.parseFloat(dqj) * Float.parseFloat(info.getStockHold()) / 
					 Float.parseFloat(info.getAllmoney()) * 100;
			 Float stock_market_price = Float.parseFloat(dqj) * Float.parseFloat(info.getStockHold());
			 holder.txt_stock_name.setText(info.getStockName());
			 holder.txt_stock_code.setText("("+info.getOrigStockId()+")");
			 
			 String formatCode = context.getResources().getString(R.string.txt_stock_name_code);
			 //盈亏金额 format: txt_stock_name_code 
			 Float breakevenamount_price = (Float.parseFloat(dqj) -Float.parseFloat(info.getStockykj())) * Float.parseFloat(info.getStockHold());
			 String ykje =decimalFormat.format( breakevenamount_price);
			 //盈亏率：
			 String ykl = decimalFormat.format (breakevenamount_price/(Float.parseFloat(info.getStockykj()) *  Float.parseFloat(info.getStockHold())) *100)+"%";
			 String item_yk = String.format(formatCode, ykje,ykl);
			 
			 SpannableStringBuilder style=new SpannableStringBuilder(item_yk);          
			 style.setSpan(new ForegroundColorSpan(
					 context.getResources().getColor( KouFuTools.getStockValueColor(ykje) ) ),0,ykje.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
			 style.setSpan(new ForegroundColorSpan(
					 context.getResources().getColor( KouFuTools.getStockValueColor(ykl) ) ),ykje.length()+1,item_yk.length()-1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     
			 holder.lv_item_breakevenamount_price.setText(style);
			  
			
			  
//			 String kbj = info.getStockbj();
//			 Log.w("~~~~~~", "~1~~"+jkp);
			 //涨跌
			 String zdl = decimalFormat.format ( (Float.parseFloat(dqj) - Float.parseFloat(zsp)) / Float.parseFloat(zsp) * 100 ) +"%";
			 String item_dqj = String.format(formatCode, dqj,zdl);
			 style=new SpannableStringBuilder(item_dqj);          
			 style.setSpan(new ForegroundColorSpan(
					 context.getResources().getColor( KouFuTools.getStockValueColor(zdl) ) ),0,dqj.length(),Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
			 style.setSpan(new ForegroundColorSpan(
					 context.getResources().getColor( KouFuTools.getStockValueColor(zdl) ) ),dqj.length()+1,item_dqj.length()-1,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);      
			 holder.lv_item_curprice_gains.setText(style);
			 
			 
			 holder.btn_charge.setText("" + decimalFormat.format(charge) + "%仓");
			 holder.lv_item_cost_value.setText(info.getStockbj());
			 holder.lv_item_stock_num_value.setText(info.getStockHold());
			 holder.lv_item_stock_market_price.setText(""+ decimalFormat.format(stock_market_price));
		} catch (NumberFormatException e) { 
			e.printStackTrace();
		} catch (NotFoundException e) { 
			e.printStackTrace();
		}
		return convertView;
	}
	static class ViewHolder  
    {   
		public ImageView btn_lv_tog;
        public TextView txt_stock_name;  
        public TextView txt_stock_code;
        public TextView btn_charge;
        public TextView lv_item_curprice_gains;
        public TextView lv_item_cost_value;
        public TextView lv_item_stock_num_value;
        public TextView lv_item_breakevenamount_price;
        public TextView lv_item_stock_market_price;
        public LinearLayout ly_lv_tog;
        public RelativeLayout ly_item_stock_num,ly_item_stock_market;
        //行 按钮
        public LinearLayout ll_item_btns;
        public Button btnBuy,btnSell,btn_market,btn_trans_details;
    }  
	 class ItemClick implements View.OnClickListener{
		 private int pos;
		public ItemClick(int pos){
			this.pos = pos;
		}
		@Override
		public void onClick(View v) { 
			UserStocks ustock = (UserStocks)getItem(pos);
			switch (v.getId()) {
			case R.id.btn_buy: 
				Intent intent = new Intent(context, BuyStocksActivity.class);
//				intent.putExtra("stockInfo", datas.get(position));
				intent.putExtra("code", ustock.getOrigStockId());
				intent.putExtra("name", ustock.getStockName());
				intent.putExtra("userStocks", stockinfo);
				intent.putExtra("datatype", 1);
				intent.putExtra("parsetype",3);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent); 
				break;
			case R.id.btn_sell: 
				intent = new Intent(context, BuyStocksActivity.class);
				intent.putExtra("stockInfo",	ustock);
				intent.putExtra("userStocks", stockinfo);
				intent.putExtra("type", 1);  
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
//				intent = new Intent(context,SellStocksListActivity.class); 
//				intent.putExtra("userStock", stockinfo);
//				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				context.startActivity(intent);
				break;
			case R.id.btn_trans_details:
				intent = new Intent(context,DetailTransStocksActivity.class);
				intent.putExtra("userStock", ustock);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				break;
			case R.id.btn_market:
				intent = new Intent(context,MarketDeatilActivity.class);
				intent.putExtra("stockCode",ustock.stockId);
				intent.putExtra("stockName",ustock.stockName);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intent);
				break;  
			default:
				break;
			}
		}
		
	}
}   
